package com.fmc123.base.utils;

import java.lang.reflect.Field;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

/**
 * 查询表单封装
* @ClassName: QueryFormUtils
* @Description: TODO 
* @author fmc
* @date 2015年11月22日下午3:40:29
*
 */
public class QueryFormUtils {

  @SuppressWarnings("unchecked")
  public static <T> T queryObject(Class<T> entityClass,HttpServletRequest request){
    Enumeration<String> attributeNames=request.getAttributeNames();
    T entity=null;
    try {
      entity = entityClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
      e.printStackTrace();
    }
    while(attributeNames.hasMoreElements()){
      String name=attributeNames.nextElement();
      Object attribute=request.getAttribute(name);
      Field file=Reflections.getAccessibleField(entity, name);
      if(null!=file){
        if(attribute instanceof String){
          attribute= ((String) attribute).trim();
        }
        Reflections.setFieldValue(entity, name, attribute);        
      }
    }
    request.setAttribute("command", entity);
    return entity;
    
  }
  
}
